<?php

/**
 * Controller_appoint
 * 活动安排
 * 
 */

class Controller_appoint extends MyApp {

	public function __construct()
	{

		parent::__construct();
        $this->model->excel    = YOU::loadModel('excel');
		$this->model->appoint  = YOU::loadModel('appoint');
		$this->model->student  = YOU::loadModel('student');
		$this->model->log      = YOU::loadModel('log');

	}
	/** 首页 **/
	public function actionIndex()
	{

	}
	public function actionOutData()
	{
		
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
        $appoint_lists = $this->db->fetchAll("SELECT * FROM `tbl_appoint_stu` ORDER BY stu_id DESC ");
        $info = array();
        foreach($appoint_lists as $key=>$value){
            // 学生姓名、学号
            $student_lists = $this->model->student->getInfo($value['student_id']);
            $appoint_lists[$key]['student_name'] =$student_lists['student_name'];
            $appoint_lists[$key]['student_no'] =$student_lists['student_no'];
            // 职位
            $leader_lists = $this->model->student->getAppInfo($value['leader_id']);
            $appoint_lists[$key]['appoint_name']=$leader_lists['leader_name'];
            $info = array(
                //编号	姓名	变动事项	日期	源单位	目的单位	在岗信息	备注	操作

                "0" => $student_lists['student_no'],
                "1" => $student_lists['student_name'],
                "2" => $leader_lists['leader_name'],


            );
            //var_dump($info);exit;
            $data_lists[$key] = $info;
        }
        //var_dump($data_lists);exit;
        $this->model->excel->outData("appoint_stu", $data_lists);
        //var_dump($appoint_lists);exit;
    }

	/** 活动安排列表 **/
	public function actionStudentList ()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_appoint_stu` ");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){	
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$appoint_lists = $this->db->fetchAll("SELECT * FROM `tbl_appoint_stu` ORDER BY stu_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach($appoint_lists as $key=>$value){
			// 学生姓名、学号
			$student_lists = $this->model->student->getInfo($value['student_id']);
			$appoint_lists[$key]['student_name'] =$student_lists['student_name'];
			$appoint_lists[$key]['student_no'] =$student_lists['student_no'];
			// 职位
			$leader_lists = $this->model->student->getAppInfo($value['leader_id']);
			$appoint_lists[$key]['appoint_name']=$leader_lists['leader_name'];
		}
	
		
		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		
		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("appoint_lists", $appoint_lists);
		$this->tpl->display('appoint/student.list.tpl.php');

	}
	// 学生职位添加
	public function actionStudentAdd()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 获取职位信息
		$appoint_lists = $this->model->appoint->getAll();
		if ($_POST){

			$student_no    	    = $this->post->student_no;
			$student_name  		= $this->post->student_name;
			$leader_id   		= $this->post->leader_id;	
			
            if (empty($student_no)) {           
            	Helper_Client::location("请填写学生学号", "index.php?a=appoint.studentadd");
            }
			if (empty($student_name)) {           
            	Helper_Client::location("请填写学生姓名", "index.php?a=appoint.studentadd");
            }
			if (empty($leader_id)) {           
            	Helper_Client::location("请填写职位", "index.php?a=appoint.studentadd");
            }
			// 判断学生是否存在
			$student_data = $this->db->fetchRow("SELECT * FROM `tbl_student` WHERE student_no='{$student_no}' AND student_name='{$student_name}'");

			if (empty($student_data)) {           
            	Helper_Client::location("学生姓名和学生学号不符，请重新填写", "index.php?a=appoint.studentadd");
            }
		
			// 写入数据库
			$info = array(
				
			"student_id"			=>$student_data['student_id'],
			"leader_id"             =>$leader_id,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_appoint_stu", $info);

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=appoint.studentlist");
		}

		$this->tpl->assign("appoint_lists", $appoint_lists);
		$this->tpl->display('appoint/student.add.tpl.php');
	}
	/** 学生职务修改 **/
	public function actionStudentUpdate()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);

		$stu_id				= $this->get->stu_id;
		// 信息
		$appoint_data = $this->db->fetchRow("SELECT * FROM `tbl_appoint_stu` WHERE stu_id={$stu_id}");
		// 获取职位信息
		$appoint_lists = $this->model->appoint->getAll();
		// 获取学生
		$student_data  = $this->model->student->getInfo($appoint_data['student_id']);
	
		if (empty($appoint_data)){
			Helper_Client::alert("当前记录不存在");
		}

		if ($_POST){

			$leader_id			= $this->post->leader_id;

			// 写入数据库
			$info = array(

			"leader_id"  			=> $leader_id,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			
			$this->db->do_update("tbl_appoint_stu", $info, "stu_id='{$stu_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=appoint.studentlist");
		}
		
		$this->tpl->assign("appoint_data", $appoint_data);
		$this->tpl->assign("appoint_lists", $appoint_lists);
		$this->tpl->assign("student_data", $student_data);
		$this->tpl->display('appoint/student.update.tpl.php');
	}
	/** 学生职务删除 **/
	public function actionStudentDel()
	{
		$stu_id	= $this->get->stu_id;
		$this->db->query("DELETE  FROM `tbl_appoint_stu` WHERE stu_id='{$stu_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=appoint.studentlist");
	}
}

?>